Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

196
Vistas
In async functions, do we return X, or do we return "Promise.Resolve(X)"?

This is something I have never quite been able to nail down.

Consider an async function in Typescript. Which is correct?

async function asyncFunctionOne(string1: string, string2: string, string3: string) {
    var returnObject1 = new object {
        fieldOne: object1,
        fieldTwo: object2,
        fieldThree: object3
    }

    return Promise.resolve(returnObject1)
}

async function asyncFunctionTwo(string1: string, string2: string, string3: string) {
    var returnObject2 = new object {
        fieldOne: object1,
        fieldTwo: object2,
        fieldThree: object3
    }

    return returnObject2;
}

Additionally, consider we were calling this function and we needed the response for something further in our program. Would we need to await the first function, the second function, both, or neither?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

That async functions always return a promise. A plain return value will be wrapped with a resolved promise always. Observe in the following code that the same result can be obtained either using await or Promise.then.

(async function() {

   async function asyncFunctionTwo() {
      return 999;
   }


   asyncFunctionTwo().then((val => console.log(val)));
   
   //OR
   
   console.log(await asyncFunctionTwo())

})();

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda